﻿$(document).ready(function () {
    var albumId = $("#AlbumId").val();
    var albumLockStatusId = $("#AlbumLockStatusId").val();
    var albumStatusId = $("#AlbumStatusId").val();

    //init the scrollbar
    $("#scrollbar").tinyscrollbar();

    //remove the left and right bars, show full main panel
    $("#content #contentContainer #mainPanel").addClass("full");
    $("#content #contentContainer").addClass("full");
    $("#content #contentContainer #leftPanel").hide();
    $("#content #contentContainer #rightPanel").hide();

    //hide the menu and sub-menu
    $("#content ul#mainMenu,#content ul#subMenu,#footer").css("visibility", "hidden");
    //--------------------------------------------------------

    function BindSections() {
        $("ul#Sections").empty();
        $(".loadingImage").show();

        //get the sections of this album
        $.post(pathPrefix + "Albums/SectionsOfAlbum", { "id": albumId }, function (result) {
            $("#sectionRowTemplate").tmpl(result).appendTo("ul#Sections");

            //if the album is locked - disable the "Approve Album" button
            enableActionButton($("#btnApproveAlbum"));

            if (albumLockStatusId == "1" || albumStatusId == "4" || albumStatusId == "5") {
                disableActionButton($("#btnApproveAlbum"));
                $("#btnApproveAlbum").attr("title", "לא ניתן לאשר בשלב זה");
            }

            //enable the Design Album button
            enableActionButton($("#btnAlbumDesigner"));
            $("#btnAlbumDesigner").attr("title", "עיצוב אלבום").attr("href", $("#btnAlbumDesigner").data("href"));

            //add right space for every 4th element in a row (there are 5 items in a row)
            //$("ul#Sections > li:nth-child(5n+4)").css("margin-right", "50px");

            //flip using CSS every 3rd element
            $("ul#Sections > li:nth-child(3n) .paper").addClass("flip");

            //update the scrollbar
            window.setTimeout(function () {
                $("#scrollbar").tinyscrollbar_update();
            }, 100);

            //set all the statuses for sections
            $("ul#Sections li").each(function () {
                var sectionData = $(this).tmplItem().data;

                var statusId = $(this).data("status-id");
                $(this).find("[name='AlbumSectionStatusId']").val(statusId);

                var lockStatusId = $(this).data("lock-status-id");
                $(this).find("[name='AlbumSectionLockStatusId']").val(lockStatusId);

                //check if this section is editable (according to status and lockStatus)
                var allowEdit = false;

                //check if album not locked
                if (albumLockStatusId != "1") {
                    //if section not locked
                    if (sectionData.AlbumSectionLockStatusId == 3) {
                        allowEdit = true;
                    }
                }

                if (allowEdit == false) {
                    //disable the Design Section button
                    var designerButton = $(this).find(".btnAlbumSectionDesigner");
                    designerButton.attr("title", "לא ניתן לעצב מקטע זה").attr("href", "#");
                    disableActionButton(designerButton);

                    //disable the Design Album button
                    disableActionButton($("#btnAlbumDesigner"));
                    $("#btnAlbumDesigner").attr("title", "לא ניתן לעצב את האלבום כולו").attr("href", "#");
                }

                //if this section is not approved by AlbumEditor
                if (statusId == "1" || statusId == "2") {
                    disableActionButton($("#btnApproveAlbum"));
                    $("#btnApproveAlbum").attr("title", "יש לאשר קודם את כל המקטעים באלבום");
                }

                //if the album status is "מאושר מרכזת" or "מאושר דפוס" or the album lock status is "נעול"
                if (albumStatusId == "4" || albumStatusId == "5" || albumLockStatusId == "1") {
                    $(this).find("select").attr("disabled", "disabled");
                }

                //show relevant Status Buttons, according to the shown status
                if (sectionData.ShownStatus == "נעול") {
                    var approveSection = $(this).find(".btnApproveSection");
                    disableActionButton(approveSection);
                    approveSection.attr("title", "לא ניתן לאשר מקטע זה");

                    disableActionButton($(this).find(".btnOpenSection"));
                }
                else if (sectionData.ShownStatus == "בעריכה") {
                    disableActionButton($(this).find(".btnOpenSection"));
                }
                else if (sectionData.ShownStatus == "מאושר") {
                    var approveSection = $(this).find(".btnApproveSection");
                    disableActionButton(approveSection);
                    approveSection.attr("title", "לא ניתן לאשר מקטע זה");

                    disableActionButton($(this).find(".btnOpenSection"));
                }
            });

            $(".loadingImage").hide();
        }, "json");
    }

    BindSections();

    $("#btnApproveAlbum").click(function () {
        var button = $(this);

        if (button.parents("ul.buttonsContainer li").hasClass("disabled") == false) {
            var mainDialog = updateDialog("אישור אלבום", "190", "400");

            //Invoke AJAX method to show all Approval Checkes
            $.post(pathPrefix + "Albums/ShowApprovalChecks", { "isSection": false }, function (result) {
                mainDialog.html(result);
            }, "html");
        }

        return false;
    });

    $("#btnApproveAllChecks").live("click", function () {
        //check that all Approval checkboxes are selected
        if ($(".approvalCheckbox").length == $(".approvalCheckbox:checked").length) {
            var data = {
                "id": $("#AlbumId").val(),
                "statusId": 4
            };

            var mainDialog = updateDialog("אישור אלבום", "190", "400");

            //Invoke AJAX method to change the album status
            $.post(pathPrefix + "Albums/ChangeAlbumStatus", data, function (result) {
                mainDialog.html(result.Value);

                window.setTimeout(function () {
                    //refresh the page
                    window.location = window.location;
                }, 500);
            }, "json");
        }
    });

    $(".btnOpenSection").live("click", function () {
        var albumSectionRow = $(this).parents("li[data-section-id]");

        //change the lock status to "פתוח לעריכה"
        albumSectionRow.find("[name='AlbumSectionLockStatusId']").val("3").change();

        return false;
    });

    $(".btnApproveSection").live("click", function () {
        var button = $(this);

        if (button.parents("ul.buttonsContainer li").hasClass("disabled") == false) {
            var albumSectionRow = $(this).parents("li[data-section-id]");
            var albumSectionData = $(this).tmplItem().data;
            var albumSectionTitle = albumSectionData.SectionTitle;

            if (albumSectionRow.find("[name='AlbumSectionLockStatusId']").prop("disabled")) {
                var mainDialog = updateDialog("שינוי סטטוס", "100", "400");
                mainDialog.html('האלבום נעול ולכן לא ניתן לשנות את הסטטוס של "' + albumSectionTitle + '".');
            }
            else {
                openConfirmDialog('האם אתה בטוח שברצונך לאשר את "' + albumSectionTitle + '"?', function () {
                    //change the status to "אושר על ידי מרכזת"
                    albumSectionRow.find("[name='AlbumSectionStatusId']").val("3").change();
                });
            }
        }

        return false;
    });

    $(".isLocked").live("click", function () {
        var albumSectionRow = $(this).parents("li[data-section-id]");
        var albumSectionLockStatusId = albumSectionRow.data("lock-status-id");
        var albumSectionData = $(this).tmplItem().data;
        var albumSectionTitle = albumSectionData.SectionTitle;

        //נעול דפוס
        if (albumSectionLockStatusId == "1") {
            return false;
        }

        if (albumSectionRow.find("[name='AlbumSectionLockStatusId']").prop("disabled")) {
            var mainDialog = updateDialog("שינוי סטטוס", "100", "400");
            mainDialog.html('האלבום נעול ולכן לא ניתן לשנות את הסטטוס של "' + albumSectionTitle + '".');
        }
        else {
            //נעול מרכזת
            if (albumSectionLockStatusId == "2") {
                openConfirmDialog('האם אתה בטוח שברצונך לפתוח את "' + albumSectionTitle + '" לעריכה?', function () {
                    //change the lock status to "פתוח לעריכה"
                    albumSectionRow.find("[name='AlbumSectionLockStatusId']").val("3").change();
                });
            }
            //פתוח לעריכה
            else if (albumSectionLockStatusId == "3") {
                openConfirmDialog('האם אתה בטוח שברצונך לנעול את  "' + albumSectionTitle + '" לעריכה?', function () {
                    //change the lock status to "נעול מרכזת"
                    albumSectionRow.find("[name='AlbumSectionLockStatusId']").val("2").change();
                });
            }
        }
    });

    function enableActionButton(button) {
        button.parents("ul.buttonsContainer li").removeClass("disabled");
    }

    function disableActionButton(button) {
        if (button.parents("ul.buttonsContainer li").hasClass("disabled") == false) {
            button.parents("ul.buttonsContainer li").addClass("disabled");
        }
    }

    /* Side Menu Actions
    ----------------------*/

    $("ul#sideMenu li.contract a").click(function () {
        var mainDialog = updateDialog("חוזה", "190", "400");

        mainDialog.html($("#contractDialog").html());

        return false;
    });

    $("ul#sideMenu li.filmingDays a").click(function () {
        var mainDialog = updateDialog("ימי צילום", "190", "400");

        mainDialog.html($("#filmingDaysDialog").html());

        return false;
    });

    $("ul#sideMenu li.printVersion a").click(function () {
        var mainDialog = updateDialog("גרסת דפוס", "190", "500");

        mainDialog.html($("#printVersionDialog").html());

        return false;
    });

    $("ul#sideMenu li.editors a").click(function () {
        var mainDialog = updateDialog("עדכון עורכים", "400", "850");
        var url = $(this).attr("href");

        //Invoke AJAX method to get the editors dialog
        mainDialog.load(url);

        return false;
    });

    /* Filming Days
    -----------------*/

    function BindFilmingDays() {
        var FilmingDaysJson = $("#FilmingDaysJSON").val();
        var FilmingDays = $.parseJSON(FilmingDaysJson);

        $("#filmingDayRowTemplate").tmpl(FilmingDays).appendTo("#FilmingDays");

        //there are no filming days - show message
        if (FilmingDays.length == 0) {
            $("#FilmingDaysContainer").hide();
        }
    }

    BindFilmingDays();

    /* Album Section Status change
    -------------------------------*/

    //when the status is being change
    $("[name='AlbumSectionStatusId']").live("change", function () {
        var albumSectionRow = $(this).parents("li[data-section-id]");
        var albumSectionId = albumSectionRow.data("section-id");
        var statusId = $(this).val();
        var statusName = $(this).find("option:selected").html();

        var data = {
            "id": albumSectionId,
            "statusId": statusId
        };

        $("ul#Sections").empty();
        $(".loadingImage").show();

        //Invoke AJAX method to change the album section status
        $.post(pathPrefix + "Albums/ChangeAlbumSectionStatus", data, function (result) {
            albumSectionRow.data("status-id", statusId);

            //if status = "אישור מרכזת"
            if (statusId == "3") {
                //change lock status to "נעול דפוס"
                albumSectionRow.find("[name='AlbumSectionLockStatusId']").val("1").change();
            }
            else {
                BindSections();
            }

        }, "json");
    });

    //when the lock status is being change
    $("[name='AlbumSectionLockStatusId']").live("change", function () {
        var albumSectionRow = $(this).parents("li[data-section-id]");
        var albumSectionId = albumSectionRow.data("section-id");
        var lockStatusId = $(this).val();

        var data = {
            "id": albumSectionId,
            "lockStatusId": lockStatusId
        };

        $("ul#Sections").empty();
        $(".loadingImage").show();

        //Invoke AJAX method to change the album section lock status
        $.post(pathPrefix + "Albums/ChangeAlbumSectionLockStatus", data, function (result) {
            //if lockStatus = "פתוח לעריכה"
            if (lockStatusId == "3") {
                //cahnge status to "לא מאושר"
                albumSectionRow.find("[name='AlbumSectionStatusId']").val("1").change();
            }
            else {
                BindSections();
            }
        }, "json");
    });
});